package com.inspur.dgov.vlbod.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.inspur.dgov.vlbod.entity.LiveCollect;
import com.inspur.dgov.vlbod.http.DgovResponse;
import com.inspur.dgov.vlbod.secure.token.UserRecord;
import com.inspur.dgov.vlbod.service.LiveCollectService;
import com.inspur.dgov.vlbod.service.UserRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * <p>
 *  直播收藏服务控制器
 * </p>
 *
 * @author chiyupei
 * @since 2023-05-29
 */
@RestController
@RequestMapping("/liveCollect")
public class LiveCollectController {

    @Value("${dgov.collect.count}")
    private Integer collectCount;

    @Autowired
    private UserRecordService userRecordService;

    @Autowired
    private LiveCollectService liveCollectService;

    /**
     * 直播收藏
     *
     * @param id 直播ID
     * @return
     */
    @GetMapping("/add/{id}")
    public DgovResponse add(HttpServletRequest request, @PathVariable String id) {
        // 获取当前登录用户ID
        UserRecord userRecord = this.userRecordService.getUserRecord(request);
        String userId = userRecord.getUserId();
        LambdaQueryWrapper<LiveCollect> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LiveCollect::getBroadcastId, id);
        queryWrapper.eq(LiveCollect::getUserId, userId);
        LiveCollect collect = this.liveCollectService.getOne(queryWrapper);
        if (null != collect) {
            return DgovResponse.success(collect);
        }
        // 检查是否已达到收藏上限
        int count = this.liveCollectService.count(new LambdaQueryWrapper<LiveCollect>().eq(LiveCollect::getUserId, userId));
        if (count >= collectCount) {
            return DgovResponse.fail("收藏已达上限！");
        }
        LiveCollect liveCollect = new LiveCollect();
        liveCollect.setBroadcastId(id);
        liveCollect.setUserId(userId);
        liveCollect.setCreateTime(LocalDateTime.now());
        this.liveCollectService.save(liveCollect);
        return DgovResponse.success(liveCollect);
    }

    /**
     * 取消直播收藏
     *
     * @param id 直播ID
     * @return
     */
    @GetMapping("/delete/{id}")
    public DgovResponse delete(HttpServletRequest request, @PathVariable String id) {
        // 获取当前登录用户ID
        UserRecord userRecord = this.userRecordService.getUserRecord(request);
        String userId = userRecord.getUserId();
        LambdaUpdateWrapper<LiveCollect> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(LiveCollect::getBroadcastId, id);
        updateWrapper.eq(LiveCollect::getUserId, userId);
        boolean result = this.liveCollectService.remove(updateWrapper);
        return DgovResponse.success(result);
    }

    /**
     * 统计直播收藏数
     *
     * @param id 直播ID
     * @return
     */
    @GetMapping("/count/{id}")
    public DgovResponse count(HttpServletRequest request, @PathVariable String id) {
        LambdaQueryWrapper<LiveCollect> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LiveCollect::getBroadcastId, id);
        int count = this.liveCollectService.count(queryWrapper);
        return DgovResponse.success(count);
    }

    /**
     * 检查当前用户是否已收藏
     *
     * @param id 直播ID
     * @return
     */
    @GetMapping("/check/{id}")
    public DgovResponse check(HttpServletRequest request, @PathVariable String id) {
        // 获取当前登录用户ID
        UserRecord userRecord = this.userRecordService.getUserRecord(request);
        String userId = userRecord.getUserId();
        LambdaQueryWrapper<LiveCollect> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LiveCollect::getUserId, userId);
        queryWrapper.eq(LiveCollect::getBroadcastId, id);
        int count = this.liveCollectService.count(queryWrapper);
        return DgovResponse.success(count > 0);
    }
}
